/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BussinessObject;

import DataAccessObject.SQLServerDAO;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ngoctoan
 */
public class HoadonBO {

    public ResultSet LayIDhd() {
        SQLServerDAO dao;
        try {
            dao = new SQLServerDAO();
            return dao.LayDuLieu("SELECT Id_Hoadon FROM tblHoadon WHERE Id_Hoadon = (SELECT MAX(Id_Hoadon) FROM tblHoadon)");
        } catch (Exception ex) {
            Logger.getLogger(NhaphangBO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public ResultSet LayMasp() {
        try {
            SQLServerDAO dao = new SQLServerDAO();
            return dao.LayDuLieu("SELECT ID_Sanpham, Masp FROM tblSanpham WHERE Soluong>0");
        } catch (Exception e) {
            System.out.println("Error: " + e);
        }
        return null;
    }

    public ResultSet LaytatcaHoadon() {
        try {
            SQLServerDAO dao = new SQLServerDAO();
            return dao.LayDuLieu("SELECT ID_Hoadon, MaHd, Ngaylap, Tongtien, Ghichu FROM tblHoadon");
        } catch (Exception e) {
            System.out.println("Error: " + e);
        }
        return null;
    }

    public ResultSet LayCTHD(Object Id_Hoadon) {
        try {
            SQLServerDAO dao = new SQLServerDAO();
            return dao.LayDuLieu("SELECT sp.Tensp, cthd.Dongia, cthd.Soluong, cthd.Thanhtien FROM tblSanpham sp JOIN tbCthd cthd ON sp.ID_Sanpham = cthd.Id_Sanpham WHERE cthd.Id_Hoadon = " + Id_Hoadon);
        } catch (Exception e) {
            System.out.println("Error: " + e);
        }
        return null;
    }

    public ResultSet LayCtsp(Object Masp) {
        try {
            SQLServerDAO dao = new SQLServerDAO();

            return dao.LayDuLieu("SELECT ID_Sanpham, Masp, Tensp, Donvitinh, Giaban FROM tblSanpham WHERE Masp = '" + Masp + "'");
        } catch (Exception e) {
            System.out.println("Error: " + e);
        }
        return null;
    }

    public int ThemHoadon(Object MaHd, Object Tongtien, Object Ghichu) {
        try {
            SQLServerDAO dao = new SQLServerDAO();
            Object p[] = new Object[4];
            //System.out.println(row+1);
            String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
            //System.out.println(timeStamp);
            p[0] = MaHd;
            p[1] = timeStamp;
            p[2] = Tongtien;
            p[3] = Ghichu;
            //System.out.println(p[1]);
            int kq = dao.UpdateQuery("INSERT INTO tblHoadon VALUES(?,?,?,?)", p);
            int returnId = 1;

            ResultSet rs = this.LayIDhd();
            while (rs.next()) {
                returnId = Integer.parseInt(rs.getString(1));
            }
            System.out.println(String.valueOf(returnId));
            return returnId;
        } catch (Exception e) {
            System.out.println("1" + e);
        }
        return 0;
    }

    public int ThemCthd(Object Id_Hoadon, Object Id_Sanpham, Object Soluong, Object Giaban, Object Thanhtien) {
        try {
            SQLServerDAO dao = new SQLServerDAO();
            Object p[] = new Object[5];
            p[0] = Id_Hoadon;
            p[1] = Id_Sanpham;
            p[2] = Soluong;
            p[3] = Giaban;
            p[4] = Thanhtien;
            int kq = dao.UpdateQuery("INSERT INTO tbCthd VALUES(?,?,?,?,?)", p);
            return kq;
        } catch (Exception ex) {
            Logger.getLogger(HoadonBO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

    public int CapNhatSPNhapHang(Object Id_Sanpham, Object Soluong) {

        SQLServerDAO dao;
        try {
            dao = new SQLServerDAO();

            Object p[] = new Object[2];
            p[0] = Soluong;
            p[1] = Id_Sanpham;
            int kq = dao.UpdateQuery("UPDATE tblSanpham SET Soluong=Soluong-? WHERE ID_Sanpham = ?", p);
            return kq;
        } catch (Exception ex) {
            Logger.getLogger(HoadonBO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }
}
